﻿using System;
using System.Linq;
using Hjt212Parser;
using HpSocketIotGateway.Db;
using HpSocketIotGateway.Db.Code1st;
using HpSocketIotGateway.Properties;
using log4net;

namespace HpSocketIotGateway.Facade
{
    public class BackupProcessedRecord
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(BackupProcessedRecord));
        /// <summary>
        /// 备份命令发送表，将已处理的指令移到对应表
        /// </summary>
        public static void BackupSentCmd()
        {
            try
            {
                using (var ctx = new GatewayContext())
                {
                    var list = ctx
                        .send_ctr_cmd
                        .Where(t=>t.is_sent)
                        //.Where(t => t.rtn_9011 != null && t.rtn_9011.Contains("O"))
                        //.Where(t => t.rtn_9012 != null && t.rtn_9012.Contains("O"))
                        //.Where(t => t.rtn_9014 != null && t.rtn_9014.Contains("O"))
                        .Take(5000)
                        .Select(t => t.id)
                        .ToList();

                    if (list.Count > 0)
                    {
                        DbRouteAndBackup.SynSendCtrCmd(list);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            // ReSharper disable once FunctionNeverReturns
        }
        /// <summary>
        ///  备份接收表，将已处理的指令移到对应表
        /// </summary>
        public static void BackupProcessedMsg()
        {
            try
            {
                var finished = ProcessFlag.ProcessingFinished.ToString();
                using (var ctx = new GatewayContext())
                {
                    var list = ctx
                        .received_msg
                        .Where(t => (t.processed_flag.Equals(finished) && t.processed_flag_2.Equals(finished) && t.processed_flag_3.Equals(finished)) || !t.is_length_right)
                        .Where(t => !t.cn.Equals("9999"))
                        .Take(5000)
                        .Select(t => t.id)
                        .ToList();

                    if (list.Count > 0 )
                    {
                        DbRouteAndBackup.SynDataReceived(list);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            try
            {
                var last = DateTime.Now.AddDays(-15);
                using (var ctx = new GatewayContext())
                {
                    var list = ctx
                        .received_msg
                        .Where(t => t.received_datetime < last)
                         .Where(t => !t.cn.Equals("9999"))
                        .Take(5000)
                        .Select(t => t.id)
                        .ToList();

                    if (list.Count > 0 )
                    {
                        DbRouteAndBackup.SynDataReceived(list);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        public static void Delete9999Or0000()
        {
            try
            {
                var finished = ProcessFlag.ProcessingFinished.ToString();
                using (var ctx = new GatewayContext())
                {
                    var list = ctx
                        .received_msg
                        .Where(t => t.cn.Equals("9999"))//|| t.cn.Equals("0000"))
                        .Where(t => t.processed_flag.Equals(finished) && t.processed_flag_2.Equals(finished) && t.processed_flag_3.Equals(finished))
                        .Take(5000)
                        .ToList();
                    foreach (var msg in list)
                    {
                        ctx.received_msg.Remove(msg);
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

            try
            {
                var last = DateTime.Now.AddDays(-7);
                using (var ctx = new GatewayContext())
                {
                    var list = ctx
                        .received_msg
                        .Where(t => t.received_datetime < last)
                        .Where(t => t.cn.Equals("9999"))//|| t.cn.Equals("0000"))
                        .Take(5000)
                        .ToList();
                    foreach (var msg in list)
                    {
                        ctx.received_msg.Remove(msg);
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }
        /// <summary>
        ///  备份数据发送表，将已处理的指令移到对应表
        /// </summary>
        public static void BackupSentData()
        {
            var last = DateTime.Now.AddDays(-3);
            try
            {
                using (var ctx = new GatewayContext())
                {
                    var list = ctx
                        .send_data
                        .Where(t=>t.is_sent || t.send_time < last)
                       // .Where(t => (t.rtn_9011 != null && t.rtn_9011.Contains("O") && t.rtn_9012 != null && t.rtn_9012.Contains("O") && t.rtn_9014 != null && t.rtn_9014.Contains("O")) || t.send_time < last)
                        .Take(5000)
                        .Select(t => t.id)
                        .ToList();
                    if (list.Count > 0 )
                        DbRouteAndBackup.SynSendData(list);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

    }
}
